//
//  ViewController.m
//  22-AliPlanet_HomeWork
//
//  Created by 徐昊 on 16/8/12.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import "AliData.h"
#import "BigData.h"
#import "BigAction.h"
#import "LittleData.h"
#import "LittleAction.h"
#import "IconTableViewCell.h"
#import "TuijianTableViewCell.h"
#import "NewSongTableViewCell.h"
#import "DujiaTableViewCell.h"
#import "GXCycleImages.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "CustomViewController.h"

#define kAliPlanetURLString @"http://api.dongting.com/frontpage/frontpage?location=0&version=1470998222737&app=ttpod&v=v9.0.4.2016070616&user_id=0&mid=iPhone8%2C1&f=f320&s=s310&imsi=&hid=&splus=10.0&active=1&net=2&openudid=4bbd8925c4a3991ee51c9cd8b9a8dc5961820764&idfa=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&utdid=V62kivWPJBYDABK%2FL8gCUnDI&alf=(null)&bundle_id=com.ttpod.music&latitude=(null)&longtitude=(null)&access_token=(null)&vc=9000400"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource, NewSongTableViewCellDelegate>
@property (nonatomic, strong) AliData *aliData;
@property (weak, nonatomic) IBOutlet UIView *playAndMusicView;
@property (weak, nonatomic) IBOutlet UILabel *zhiboLabel;
@property (weak, nonatomic) IBOutlet UILabel *musicLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet GXCycleImages *cycleView;
@property (nonatomic, strong) NSMutableArray *imagesArray;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, strong) NSIndexPath *indexPath1;
@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestDataWithURL:kAliPlanetURLString];
    [self config];
    _cycleView.placeholderImage = [UIImage imageNamed:@"0"];
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.shadowImage = [UIImage new];
}

- (void)config {
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.shadowImage = [UIImage new];
//    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bar"]];
//    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bar"]];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"back"]];
    _playAndMusicView.layer.cornerRadius = 13;
    _playAndMusicView.layer.borderColor = [UIColor whiteColor].CGColor;
    _playAndMusicView.layer.borderWidth = 1;
    _tableView.tableHeaderView = _cycleView;
}

- (void)requestDataWithURL:(NSString *)urlString {
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer = [AFHTTPResponseSerializer serializer];
    [session GET:urlString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError *error = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        self.aliData = [AliData modelWithDictionary:dic];
        self.imagesArray = [NSMutableArray array];
        for (LittleData *data in _aliData.data[0].data) {
            [_imagesArray addObject:data.picUrl];
        }
        _cycleView.imagesArray = _imagesArray;
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
//    NSURLSession *session = [NSURLSession sharedSession];
//    NSURL *url = [NSURL URLWithString:urlString];
//    NSMutableURLRequest *hotRequest = [NSMutableURLRequest requestWithURL:url];
//    hotRequest.HTTPMethod = @"GET";
//    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:hotRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        dispatch_async(dispatch_get_main_queue(), ^{
//            NSError *error = nil;
//            NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data
//                                                                       options:NSJSONReadingMutableContainers
//                                                                         error:&error];
//            self.aliData = [AliData modelWithDictionary:dictionary];
//            self.imagesArray = [NSMutableArray array];
//            for (LittleData *data in _aliData.data[0].data) {
//                [_imagesArray addObject:data.picUrl];
//            }
//            _cycleView.imagesArray = _imagesArray;
//            [self.tableView reloadData];
//        });
//    }];
//    [dataTask resume];
}
#pragma mark - CustomViewCotroller 协议方法
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    CustomViewController *vc = segue.destinationViewController;
    vc.str = _aliData.data[_indexPath1.section].data[_indexPath.row].action.value;
    vc.num = _indexPath.row;
}
- (void)newSong:(NSIndexPath *)indexPath {
    _indexPath1 = [self.tableView indexPathForSelectedRow];
    _indexPath = indexPath;
}
#pragma mark - tableView协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _aliData.data.count - 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 6) {
        return _aliData.data[section + 1].data.count;
    } else {
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        IconTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"icon"];
        cell.data = _aliData.data[indexPath.section + 1].data;
        return cell;
    } else if (indexPath.section == 1) {
        TuijianTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tuijian"];
        cell.titleLabel.text = _aliData.data[indexPath.section + 1].name;
        cell.data = _aliData.data[indexPath.section + 1].data;
        return cell;
    } else if (indexPath.section == 6) {
        DujiaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dujia"];
        LittleData *littleData = _aliData.data[indexPath.section + 1].data[indexPath.row];
        cell.data = littleData;
        return cell;
    } else {
        NewSongTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newsong"];
        cell.titleLabel.text = _aliData.data[indexPath.section + 1].name;
        cell.data = _aliData.data[indexPath.section + 1].data;
        if (cell.data.count > 3) {
            cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:(arc4random() % 10) / 10.0f];
        }
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 60;
    } else if (indexPath.section == 1) {
        return 100;
    } else if (indexPath.section == 2 || indexPath.section == 5) {
        return 180;
    } else if (indexPath.section == 3 || indexPath.section == 4) {
        return 330;
    } else {
        return 60;
    }
}
#pragma mark - Other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
